<?php
namespace app\merchant\controller\order;
use app\common\controller\BaseController;
use app\common\model\order\ShopOrderCommentModel;
use app\common\model\order\ShopOrderModel;
use app\common\model\product\ShopProductTypeModel;
use app\common\model\setting\SettingPriceRangeModel;
use app\common\model\setting\SettingRefundModel;
use app\common\service\appeal\ShopOrderAppealService;
use app\common\service\order\ShopOrderCommentService;
use app\common\service\order\ShopOrderService;
use app\common\validate\appeal\ShopOrderAppealValidate;
use app\common\validate\order\ShopOrderValidate;
use hg\apidoc\annotation as Apidoc;
/**
 * @Apidoc\Title("订单管理")
 * @Apidoc\Group("order")
 * @Apidoc\Sort("250")
 */
class ShopOrder extends BaseController
{
    /**
    * @Apidoc\Title("订单管理列表")
    * @Apidoc\Query(ref="pagingQuery")
    * @Apidoc\Query(ref="sortQuery")
    * @Apidoc\Query(ref="searchQuery")
    * @Apidoc\Query(ref="dateQuery")
    * @Apidoc\Returned(ref="expsReturn")
    * @Apidoc\Returned(ref="pagingReturn")
    * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
    *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
    * })
    */
    public function list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'status',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }
    /**
     * @Apidoc\Title("浏览管理列表")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function browse_list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'status',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        $where[] = ['shop_product_type_code','=',ShopProductTypeModel::LLRW];
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order(),'',3);
        return success($data);
    }

    /**
     * @Apidoc\Title("发布评价管理列表")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function comment_list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $merchant_id=mer_id();
        $where[] = ['merchant_id','=',$merchant_id];
        $where[] = ['enter.comment_type','=',SettingPriceRangeModel::getCommentType('WXPJ',2)];
        //查询指定状态下的订单id
        $param = $this->params(['comment_status/d' => 0]);
        if($param['comment_status']>0){
            $comment_where[] = ['is_delete','=',0];
            $comment_where[] = ['comment_status','=',2];
            $comment_where[] = ['status','=',$param['comment_status']];
            $comment_where[] = ['merchant_id','=',$merchant_id];
            $order_ids = ShopOrderCommentService::getShopOrderIds($comment_where);
            if($order_ids){
                $where[] = ['id','in',$order_ids];
            }else{
                $where[] = ['id','=',0];
            }
        }else{
            $where[] = ['status','<',ShopOrderModel::getStatus('o_auth',1)];
        }
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }
    /**
     * @Apidoc\Title("追评价管理列表")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function comment_add_list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $merchant_id=mer_id();
        $where[] = ['merchant_id','=',$merchant_id];
        $where[] = ['status','=',ShopOrderModel::getStatus('o_success',1)];
        //查询指定状态下的订单id
        $param = $this->params(['comment_status/d' => 0]);
        if($param['comment_status']>0){
            $comment_where[] = ['is_delete','=',0];
            $comment_where[] = ['comment_status','=',3];
            $comment_where[] = ['status','=',$param['comment_status']];
            $comment_where[] = ['merchant_id','=',$merchant_id];
            $order_ids = ShopOrderCommentService::getShopOrderIds($comment_where);
            if($order_ids){
                $where[] = ['id','in',$order_ids];
            }else{
                $where[] = ['id','=',0];
            }
        }
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order());
        return success($data);
    }
    /**
     * @Apidoc\Title("商家返款列表")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function merchant_refund_list()
    {
        $where = $this->buildWhere([
            'is_disable',
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id',
            'mer_refund_status'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        //获取商家返款ID,商家返款，需要商家手动返给买家本金
        $setting_refund_id = SettingRefundModel::getRefundType(SettingRefundModel::SJFK,1);
        $where[] = ['setting_refund_id','=',$setting_refund_id];

        //订单状态：待发货、待评价、评价待审核、完成
        $where[] = ['status','in',[
//            ShopOrderModel::getStatus('o_delivery',1),
            ShopOrderModel::getStatus('o_evaluate',1),
            ShopOrderModel::getStatus('o_auth',1),
            ShopOrderModel::getStatus('o_success',1),
        ]];
        //查询字段
        $field = 'platform.title as platform_title,
            platform.image_id as platform_image_id,
            type.title as type_title,
            shop.title as shop_title,
            refund.title as refund_title,
            goods.id as goods_id,
            goods.third_id as goods_third_id,
            goods.goods_title,
            goods.image_id as goods_image_id,
            goods.goods_spec,
            goods.goods_type,
            product.h_pay_time,
            enter.comment_type,
            enter.enter_type,
            enter.keyword as enter_keyword,
            enter.qr_image_id as enter_qr_image_id,
            account.consignee as ac_consignee,
            account.phone as ac_phone,
            account.region_id as ac_region_id,
            account.address as ac_address,
            account.accname,
            member.alipay_account as member_alipay_account,
            member.alipay_image_id as member_alipay_image_id,
            member.wechat_image_id as member_wechat_image_id,
            member.bank_card_on as member_bank_card_on,
            member.bank_branch as member_bank_branch,
            member.bank_title as member_bank_title,
            member.name as member_name,
            a.*';
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order(),$field,4);
        return success($data);
    }

    /**
     * @Apidoc\Title("导出支付宝转账模板")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function merRefundDownload()
    {
        $where = $this->buildWhere([
            'mer_refund_status'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        //获取商家返款ID,商家返款，需要商家手动返给买家本金
        $setting_refund_id = SettingRefundModel::getRefundType(SettingRefundModel::SJFK,1);
        $where[] = ['setting_refund_id','=',$setting_refund_id];

        //订单状态：待发货、待评价、评价待审核、完成
        $where[] = ['status','in',[
//            ShopOrderModel::getStatus('o_delivery',1),
            ShopOrderModel::getStatus('o_evaluate',1),
            ShopOrderModel::getStatus('o_auth',1),
            ShopOrderModel::getStatus('o_success',1),
        ]];
        //查询字段
        $field = '
            member.alipay_account as member_alipay_account,
            member.name as member_name,
            a.id,
            a.principal,
            a.mer_refund_msg';
        $data = ShopOrderService::list($where, 0,0, $this->order(),$field,4);
        $title = '支付宝转账'.date('Y年m月d日H时i分');
        return excelExport(
            $title,
            [[$title],['订单ID','收款方支付宝账号','收款方姓名','金额','备注']],
            $data,
            ['id','member_alipay_account','member_name','principal','mer_refund_msg'],
            ['A1:E1'],
            [],
            ['principal'],
            false,
            ['member_alipay_account','member_name','mer_refund_msg']
        );
    }
    /**
     * @Apidoc\Title("查询评价管理参数")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function getCommentParams()
    {
        $data = ShopOrderService::getCommentParams();
        return success($data);
    }
    /**
     * @Apidoc\Title("查询待自选订单")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function getSelfOrders()
    {
        $where = $this->buildWhere([
            'platform_ids',
            'product_type_ids',
            'merchant_shop_id',
            'setting_refund_id'
        ]);
        $where = $this->where(where_delete($where));
        $where[] = ['merchant_id','=',mer_id()];
        $where[] = ['status','in',[
//            ShopOrderModel::getStatus('o_listing',1),
            ShopOrderModel::getStatus('o_select',1)
        ]];
        $where[] = ['is_self_selection','=',1];
        $where[] = ['member_id','is','null'];
        $where[] = ['member_account_id','is','null'];
        $where[] = ['pending_time','<',date('Y-m-d H:i:s')];
        $data = ShopOrderService::list($where, $this->page(), $this->limit(), $this->order(),'',2);
        return success($data);
    }
    /**
    * @Apidoc\Title("订单管理信息")
    * @Apidoc\Query(ref="app\common\model\order\ShopOrderModel", field="id")
    * @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel")
    */
    public function info()
    {
        $param = $this->params(['id/d' => '']);
        validate(ShopOrderValidate::class)->scene('info')->check($param);
        $where = [
            ['id','=',$param['id']],
            ['is_delete','=',0],
            ['merchant_id','=',mer_id()]
        ];
        $data = ShopOrderService::info($where);
        return success($data);
    }

    /**
     * @Apidoc\Title("订单日志")
     * @Apidoc\Query(ref="app\common\model\order\ShopOrderModel", field="id")
     * @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel")
     */
    public function getLog()
    {
        $param = $this->params(['id/d' => '']);
        validate(ShopOrderValidate::class)->scene('info')->check($param);
        $data = ShopOrderService::getLog($param['id']);
        return success($data);
    }
    /**
    * @Apidoc\Title("订单管理添加")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="title,content,is_disable")
    */
    public function add()
    {
        $param = $this->params(ShopOrderService::$edit_field);
        validate(ShopOrderValidate::class)->scene('add')->check($param);
        $data = ShopOrderService::add($param);
        return success($data);
    }
    /**
    * @Apidoc\Title("订单管理修改")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable")
    */
    public function edit()
    {
        $param = $this->params(ShopOrderService::$edit_field);
        $type = $this->param('type/d', "");
        validate(ShopOrderValidate::class)->scene('edit'.$type)->check($param);
        if($type==1){
            $where = [
                ['id','=',$param['id']],
                ['is_delete','=',0],
                ['merchant_id','=',mer_id(true)],
            ];
            $update_data = [
                'actual_amount'=>$param['actual_amount'],
                'update_uid'=>mer_id(true),
            ];
            $data = ShopOrderService::customEdit($where, $update_data);
        }else{
            $data = ShopOrderService::edit($param['id'], $param);
        }
        return success($data);
    }
    /**
    * @Apidoc\Title("订单管理删除")
    * @Apidoc\Method("POST")
    * @Apidoc\Param(ref="idsParam")
    */
    public function dele()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopOrderValidate::class)->scene('dele')->check($param);
        $data = ShopOrderService::dele($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("订单管理禁用")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="is_disable")
     */
    public function disable()
    {
        $param = $this->params(['ids/a' => [], 'is_disable/d' => 0]);
        validate(ShopOrderValidate::class)->scene('disable')->check($param);
        $data = ShopOrderService::edit($param['ids'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("订单催评")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function urge()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopOrderValidate::class)->scene('urge')->check($param);
        $data = ShopOrderService::urge($param['ids']);
        return success($data);
    }
    /**
     * @Apidoc\Title("订单发货")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     */
    public function shipping()
    {
        $param = $this->params(['ids/a' => []]);
        validate(ShopOrderValidate::class)->scene('shipping')->check($param);
        $data = ShopOrderService::shipping($param['ids']);
        return success($data);
    }

    /**
     * @Apidoc\Title("订单审核")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="is_disable")
     */
    public function auth()
    {
        $param = $this->params(['ids/a' => [], 'auth_status/d' => 0, 'auth_msg/s' => null]);
        validate(ShopOrderValidate::class)->scene('auth')->check($param);
        $data = ShopOrderService::auth($param['ids'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("自选买号匹配订单")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable")
     */
    public function matchingOrder()
    {
        $param = $this->params(['ids/a' => [], 'member_account_ids/a' => []]);
        validate(ShopOrderValidate::class)->scene('matchingOrder')->check($param);
        $data = ShopOrderService::matchingOrder($param['ids'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("订单申诉")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable")
     */
    public function appeal()
    {
        $param = $this->params(ShopOrderAppealService::$edit_field);
        validate(ShopOrderAppealValidate::class)->scene('add')->check($param);
        $param['is_terminal'] = 1;
        $data = ShopOrderService::appeal($param['shop_order_id'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("修改关键词或者二维码")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable")
     */
    public function editKeyword()
    {
        $param = $this->params(['shop_product_enter_id/d' => 0, 'enter_type/d' => 0,'keyword/s'=>'','enter_qr_image_id/d'=>0]);
        validate(ShopOrderValidate::class)->scene('editKeyword')->check($param);
        $data = ShopOrderService::editKeyword($param['shop_product_enter_id'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("添加发布评论/追加评论")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable")
     */
    public function addComment()
    {
        $param = $this->params([
            'shop_order_id/d' => 0,
            'setting_price_range_id/d' => 0,
            'comment_status/d'=>2,
            'comment_type/d'=>2,
            'comment_type_code/s'=>'',
            'content/s'=>'',
            'comment_image_id/a'=>[],
            'comment_vido_id/d'=>null,
            'push_time/d'=>0,
            'is_anonymous/d'=>0,
            'is_close_comments/d'=>0,
        ]);
        validate(ShopOrderValidate::class)->scene('addComment')->check($param);
        $data = ShopOrderService::addComment($param['shop_order_id'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("好评审核")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="is_disable")
     */
    public function commentAuth()
    {
        $param = $this->params(['ids/a' => [], 'auth_status/d' => 0, 'auth_msg/s' => null,'comment_status/d'=>0]);
        validate(ShopOrderValidate::class)->scene('commentAuth')->check($param);
        $data = ShopOrderService::commentAuth($param['ids'], $param);
        return success($data);
    }
    /**
     * @Apidoc\Title("撤销评论")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="is_disable")
     */
    public function revokeComment()
    {
        $param = $this->params(['ids/a' => [],'comment_status/d'=>0]);
        validate(ShopOrderValidate::class)->scene('revokeComment')->check($param);
        $data = ShopOrderService::revokeComment($param['ids'], $param);
        return success($data);
    }

    /**
     * @Apidoc\Title("查询商家返款参数")
     * @Apidoc\Query(ref="pagingQuery")
     * @Apidoc\Query(ref="sortQuery")
     * @Apidoc\Query(ref="searchQuery")
     * @Apidoc\Query(ref="dateQuery")
     * @Apidoc\Returned(ref="expsReturn")
     * @Apidoc\Returned(ref="pagingReturn")
     * @Apidoc\Returned("list", type="array", desc="订单管理列表", children={
     *   @Apidoc\Returned(ref="app\common\model\order\ShopOrderModel", field="id,title,content,is_disable,create_time,update_time")
     * })
     */
    public function getMerRefundParams()
    {
        $data = ShopOrderService::getMerRefundParams();
        return success($data);
    }

    /**
     * @Apidoc\Title("商家返款审核")
     * @Apidoc\Method("POST")
     * @Apidoc\Param(ref="idsParam")
     * @Apidoc\Param(ref="app\common\model\order\ShopOrderModel", field="is_disable")
     */
    public function merRefundAuth()
    {
        $param = $this->params(['ids/a' => [], 'mer_refund_status/d' => 0, 'mer_refund_msg/s' => null,'mer_refund_type/d'=>1]);
        validate(ShopOrderValidate::class)->scene('merRefundAuth')->check($param);
        $data = ShopOrderService::merRefundAuth($param['ids'], $param);
        return success($data);
    }
}
